﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BookExchange.ODataService;
using BookExchange.ViewModel;


namespace BookExchange.Business
{
    public class CommentBusiness : BaseBusiness<Comment>
    {
        private MemberBusiness memberBus;

        public CommentBusiness()
        {
            this.memberBus = new MemberBusiness();
        }

        public override void Insert(Comment obj)
        {
            if (obj == null)
            {
                return;
            }

            DB.AddToComments(obj);
            DB.SaveChanges();
        }

        public Comment GetCommentByCode(int commentCode)
        {
            var comment = DB.Comments.Where(r => r.CommentCode == commentCode).FirstOrDefault();

            return comment;
        }

        public List<Comment> GetCommentByRequestCode(int requestCode)
        {
            var query = this.DB.Comments.Where(item => item.Active == 1 && item.RequestCode == requestCode);

            return query.ToList();
        }

        public List<CommentViewModel> GetCommentViewModelByRequestCode(int requestCode)
        {
            List<Comment> comments = GetCommentByRequestCode(requestCode);

            List<CommentViewModel> commentViewModels = comments.Select(item => new CommentViewModel(item)).ToList();

            commentViewModels.ForEach(c => c.MemberName = memberBus.GetMemberByCode(c.MemberCode).DisplayName);

            return commentViewModels;
        }

    }
}